package com.sniper.web.shiro.web.bind.method;

import com.sniper.web.shiro.web.bind.annotation.CurrentUser;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.core.MethodParameter;
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * <p>
 * 用于绑定@FormModel的方法参数解析器
 * <p>
 * User: Zhang Kaitao
 * <p>
 * Date: 13-1-12 下午5:01
 * <p>
 * Version: 1.0
 */
public class CurrentUserMethodArgumentResolver implements
        HandlerMethodArgumentResolver {
    public CurrentUserMethodArgumentResolver() {
    }

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        if (parameter.hasParameterAnnotation(CurrentUser.class)) {
            return true;
        }
        return false;
    }

    public Object resolveArgument(MethodParameter parameter,
                                  ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
                                  WebDataBinderFactory binderFactory) throws Exception {
        CurrentUser currentUserAnnotation = parameter
                .getParameterAnnotation(CurrentUser.class);
        return webRequest.getAttribute(
                currentUserAnnotation.value().toString(),
                RequestAttributes.SCOPE_REQUEST);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, Message<?> message)
            throws Exception {
        CurrentUser currentUserAnnotation = parameter
                .getParameterAnnotation(CurrentUser.class);
        System.out.println(currentUserAnnotation.value().toString());
        return null;
    }

    public static void main(String[] args) {
        System.out.println(DigestUtils.sha1Hex("admin@89013372"));

    }

}
